package tests;


import commons.Utils;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;

/**
 * Created with IntelliJ IDEA.
 * Description :
 * User: jiume
 * Date: 2025-09-10
 * Time: 14:35
 */
// 注册页面UI自动化测试
public class RegisterPage {

    public static final String url = "http://134.175.206.246:9090/register.html";

    public static WebDriver driver;

    public RegisterPage() {
        // 1、创建浏览器驱动对象
        driver = createWebDriver();

        // 2、设置隐式等待
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(2));

        // 3、访问注册页面
        driver.get(url);
    }


    /**
     * 模拟注册成功的场景
     */
    public void RegisterSuccess() throws IOException {
        // 先清空输入框
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#confirmPassword")).clear();

        // 测试用例：符合要求的用户名、密码、确认密码
        // 用户名：test123456、密码：123456、确认密码：123456
        driver.findElement(By.cssSelector("#username")).sendKeys("test123456");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#confirmPassword")).sendKeys("123456");
        driver.findElement(By.cssSelector("#registerForm > button")).click();

        // 等待弹窗出现
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(2));
        wait.until(ExpectedConditions.alertIsPresent());

        // 切换到弹窗
        Alert alert = driver.switchTo().alert();
        String warning = alert.getText();
//        System.out.println(warning);
        assert "注册成功！欢迎加入我们！".equals(warning);
        alert.accept();

        Utils.getScreenShot(driver, "RegisterSuccess");
    }


    /**
     * 模拟注册失败的场景
     */
    public void RegisterFail() throws IOException {
        // 测试用例1：不符合要求的账号，符合要求的密码、确认密码
        // 用户名：test、密码：123456、确认密码：123456
        driver.findElement(By.cssSelector("#username")).sendKeys("test");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#confirmPassword")).sendKeys("123456");
        driver.findElement(By.cssSelector("#registerForm > button")).click();

        // 等待弹窗出现
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(2));
        wait.until(ExpectedConditions.alertIsPresent());

        // 切换到弹窗
        Alert alert = driver.switchTo().alert();
        String warning = alert.getText();
//        System.out.println(warning);
        assert "注册失败! 用户名或密码或确认密码的长度小于6位".equals(warning);
        alert.accept();

        // 清空输入框
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#confirmPassword")).clear();

        // 测试用例2：符合要求的账号、不符合要求的密码，符合要求的确认密码
        // 用户名：test123456、密码：123、确认密码：123456
        driver.findElement(By.cssSelector("#username")).sendKeys("test123456");
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        driver.findElement(By.cssSelector("#confirmPassword")).sendKeys("123456");
        driver.findElement(By.cssSelector("#registerForm > button")).click();

        // 等待弹窗出现
        wait.until(ExpectedConditions.alertIsPresent());

        // 切换到弹窗
        alert = driver.switchTo().alert();
        warning = alert.getText();
//        System.out.println(warning);
        assert "注册失败! 密码和确认密码不一致".equals(warning);
        alert.accept();

        Utils.getScreenShot(driver, "RegisterFail");
    }


    /**
     * 检查页面元素是否正常显示
     */
    public void checkRegisterPageRight() throws IOException {
        // 强制等待页面元素渲染完成
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        String app_title = driver.findElement(By.cssSelector("body > div > div.app-title")).getText();
        String auth_title = driver.findElement(By.cssSelector("body > div > h1")).getText();
        String username = driver.findElement(By.cssSelector("#username")).getAttribute("placeholder");
        String password = driver.findElement(By.cssSelector("#password")).getAttribute("placeholder");
        String confirmPassword = driver.findElement(By.cssSelector("#confirmPassword")).getAttribute("placeholder");
        String button = driver.findElement(By.cssSelector("#registerForm > button")).getText();
        String markedWord = driver.findElement(By.cssSelector("body > div > div.switch-link")).getText();

//        System.out.println(app_title);
//        System.out.println(auth_title);
//        System.out.println(username);
//        System.out.println(password);
//        System.out.println(confirmPassword);
//        System.out.println(button);
//        System.out.println(markedWord);

        assert "音乐播放器".equals(app_title) && "创建账号".equals(auth_title)
                && "请输入用户名".equals(username) && "请输入密码".equals(password)
                && "请确认密码".equals(confirmPassword) && "注册账号".equals(button)
                && "已有账号? 登录".equals(markedWord);

        Utils.getScreenShot(driver, "checkRegisterPageRight");
    }


    /**
     * 创建浏览器驱动对象
     * @return 返回浏览器驱动对象
     */
    private WebDriver createWebDriver() {
        if (driver == null) {
            // 1、下载浏览器驱动
            WebDriverManager.chromedriver().setup();
            // 2、添加配置选项：
            // 允许访问所有链接
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--remote-allow-origins=*");
            // 也可以禁用自动化提示条
            options.setExperimentalOption("excludeSwitches", new String[]{"enable-automation", "enable-logging"});
            options.setExperimentalOption("useAutomationExtension", false);
            // 3、创建浏览器驱动对象
            driver = new ChromeDriver(options);
        }
        return driver;
    }


    /**
     * 退出浏览器
     */
    public void quit() {
        driver.quit();
    }
}
